﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unis.Framework.Core.Utility;

using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.CompletionMaterialsDAL.BcdProjectFileTempService;
using KingRoad.EPM.CompletionMaterialsDAL.BcdProjectFileService;
using KingRoad.EPM.CompletionMaterialsDAL.BcdFileDataService;
using KingRoad.EPM.CompletionMaterialsDAL.BcdProfiletypeService;


namespace KingRoad.EPM.CompletionMaterialsBLL.UIRules
{
    /// <summary> 
    /// 工程项目与材料类型关系表UIRule（临时表） 
    /// </summary> 
    public class BcdProjectfiletempUIRule
    {
        private readonly IBcdProjectfiletempService _bcdProjectfiletempService;
        private BcdFiledataUIRule _bcdFiledataUIRule;
        private BcdProjectfileUIRule _bcdProjectfileUIRule;
        private BcdProfiletypeUIRule _bcdProfiletypeUIRule;

        public BcdProjectfiletempUIRule() { }

        /// <summary> 
        /// construct function... 
        /// </summary> 
        public BcdProjectfiletempUIRule(IBcdProjectfiletempService bcdProjectfiletempService)
        {
            _bcdProjectfiletempService = bcdProjectfiletempService;
        }

        /// <summary>
        /// 根据条件获取信息
        /// </summary>
        /// <returns></returns>
        public List<BcdProjectfiletempInfo> GetEntityListBySql(string sql)
        {
            var ret = _bcdProjectfiletempService.GetEntityList(sql);
            return ret;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertEntity(BcdProjectfiletempInfo entity)
        {
            var ret = _bcdProjectfiletempService.InsertEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(BcdProjectfiletempInfo entity)
        {
            var ret = _bcdProjectfiletempService.UpdateEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteEntity(BcdProjectfiletempInfo info)
        {
            var ret = _bcdProjectfiletempService.DeleteEntity(info);
            return ret > 0;
        }

        /// <summary>
        /// 将临时表中的信息勾选
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="pId">弹出框的父类型</param>
        /// <param name="id">本次勾选的类型id</param>
        /// <returns></returns>
        public bool AddTemp(string proId, string pId, string fid)
        {
            int ret = 0;
            _bcdFiledataUIRule = new BcdFiledataUIRule(new BcdFiledataService());
            //获取该类型的所有子类型
            string fids = fid + "," + _bcdFiledataUIRule.GetAllIdsById(fid);
            //获取该类型的所有父类型
            string pfids = _bcdFiledataUIRule.GetAllPIdsById(pId, fid);
            string ids = (fids + "," + pfids).Trim(',');
            foreach (string id in ids.Split(','))
            {
                List<BcdProjectfiletempInfo> pfList = _bcdProjectfiletempService.GetEntityList(" P_ID='" + proId + "' and Fd_ID='" + id + "' ");
                //判断临时表中有没有这条信息（有则修改、无则添加）
                if (pfList.Count > 0)
                {
                    //修改临时表
                    BcdProjectfiletempInfo pfInfo = pfList[0];
                    if (pfInfo.P_Fd_State != null)
                    {
                        pfInfo.P_Fd_State = true;
                        ret = _bcdProjectfiletempService.UpdateEntity(pfInfo);
                    }
                }
                else
                {
                    //添加临时表
                    BcdProjectfiletempInfo pfInfo = new BcdProjectfiletempInfo();
                    pfInfo.P_ID = proId;
                    pfInfo.Fd_ID = id;
                    ret = _bcdProjectfiletempService.InsertEntity(pfInfo);
                }
            }
            return ret > 0;
        }
        /// <summary>
        /// 将临时表中的信息排除
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="fid">类型id</param>
        /// <returns></returns>
        public bool DeleteTemp(string proId, string fid)
        {
            int ret = 0;
            _bcdFiledataUIRule = new BcdFiledataUIRule(new BcdFiledataService());
            string fids = fid + "," + _bcdFiledataUIRule.GetAllIdsById(fid);
            foreach (string id in fids.Split(','))
            {
                List<BcdProjectfiletempInfo> pfList = _bcdProjectfiletempService.GetEntityList(" P_ID='" + proId + "' and Fd_ID='" + id + "' ");
                if (pfList.Count > 0)
                {
                    BcdProjectfiletempInfo pfInfo = pfList[0];
                    if (pfInfo.P_Fd_State == null)
                    {
                        //删除临时表中的数据
                        ret = _bcdProjectfiletempService.DeleteEntity(pfInfo);
                    }
                    else
                    {
                        //修改临时表中数据，将数据标注为删除 以备保存时用
                        pfInfo.P_Fd_State = false;
                        ret = _bcdProjectfiletempService.UpdateEntity(pfInfo);
                    }
                }
            }
            return ret > 0;
        }
        /// <summary>
        /// 根据proId删除 临时表中 所有有关该项目的信息
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <returns></returns>
        public bool DeleteByPID(string proId)
        {
            var ret = _bcdProjectfiletempService.DeleteByPID(proId);
            return ret > 0;
        }
        /// <summary>
        /// 根据proId将临时表中的信息 保存到工程项目与材料类型关系表
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public bool SelSave(string proId)
        {
            //获取待添加列表
            List<BcdProjectfiletempInfo> aList = _bcdProjectfiletempService.GetEntityList(" P_ID='" + proId + "' and P_Fd_State is null ");
            _bcdProjectfileUIRule = new BcdProjectfileUIRule(new BcdProjectfileService());
            foreach (BcdProjectfiletempInfo ainfo in aList)
            {
                //添加
                BcdProjectfileInfo info = new BcdProjectfileInfo();
                info.P_ID = proId;
                info.Fd_ID = ainfo.Fd_ID;
                _bcdProjectfileUIRule.InsertEntity(info);
            }
            //获取待删除列表
            List<BcdProjectfiletempInfo> dList = _bcdProjectfiletempService.GetEntityList(" P_ID='" + proId + "' and P_Fd_State=0 ");
            foreach (BcdProjectfiletempInfo dInfo in dList)
            {
                //删除
                BcdProjectfileInfo info = new BcdProjectfileInfo();
                info.P_ID = proId;
                info.Fd_ID = dInfo.Fd_ID;
                _bcdProjectfileUIRule.DeleteEntity(info);
            }

            //保存后清除临时表信息
            var ret = _bcdProjectfiletempService.DeleteByPID(proId);
            return ret > 0;
        }

        /// <summary>
        /// 将临时表中的信息勾选
        /// </summary>
        /// <param name="tempId">临时表id</param>
        /// <param name="pId">父类型id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public bool TempAdd(string tempId, string pId, string id)
        {
            int ret = 0;
            _bcdFiledataUIRule = new BcdFiledataUIRule(new BcdFiledataService());
            //获取该类型的所有子类型
            string fids = id + "," + _bcdFiledataUIRule.GetAllIdsById(id);
            //获取该类型的所有父类型
            string pfids = _bcdFiledataUIRule.GetAllPIdsById(pId, id);
            string ids = (fids + "," + pfids).Trim(',');
            foreach (string sid in ids.Split(','))
            {
                List<BcdProjectfiletempInfo> pfList = _bcdProjectfiletempService.GetEntityList(" P_ID='" + tempId + "' and Fd_ID='" + sid + "' ");
                //判断临时表中有没有这条信息（有则修改、无则添加）
                if (pfList.Count > 0)
                {
                    //修改临时表
                    BcdProjectfiletempInfo pfInfo = pfList[0];
                    if (pfInfo.P_Fd_State != null)
                    {
                        pfInfo.P_Fd_State = true;
                        ret = _bcdProjectfiletempService.UpdateEntity(pfInfo);
                    }
                }
                else
                {
                    //添加临时表
                    BcdProjectfiletempInfo pfInfo = new BcdProjectfiletempInfo();
                    pfInfo.P_ID = tempId;
                    pfInfo.Fd_ID = sid;
                    ret = _bcdProjectfiletempService.InsertEntity(pfInfo);
                }
            }
            return ret > 0;
        }

        /// <summary>
        /// 将临时表中的信息排除
        /// </summary>
        /// <param name="tempId">临时表id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public bool TempDelete(string tempId, string id)
        {
            int ret = 0;
            _bcdFiledataUIRule = new BcdFiledataUIRule(new BcdFiledataService());
            string fids = id + "," + _bcdFiledataUIRule.GetAllIdsById(id);
            foreach (string sid in fids.Split(','))
            {
                List<BcdProjectfiletempInfo> pfList = _bcdProjectfiletempService.GetEntityList(" P_ID='" + tempId + "' and Fd_ID='" + sid + "' ");
                if (pfList.Count > 0)
                {
                    BcdProjectfiletempInfo pfInfo = pfList[0];
                    if (pfInfo.P_Fd_State == null)
                    {
                        //删除临时表中的数据
                        ret = _bcdProjectfiletempService.DeleteEntity(pfInfo);
                    }
                    else
                    {
                        //修改临时表中数据，将数据标注为删除 以备保存时用
                        pfInfo.P_Fd_State = false;
                        ret = _bcdProjectfiletempService.UpdateEntity(pfInfo);
                    }
                }
            }
            return ret > 0;
        }

        /// <summary>
        /// 根据proId将临时表中的信息 保存到工程项目竣工资料材料类型表
        /// </summary>
        /// <param name="tempId">临时表id</param>
        /// <param name="pftId">项目材料类型id</param>
        /// <param name="id">类型id</param>
        /// <param name="userId">登录人id</param>
        /// <param name="userName">登录人名称</param>
        /// <returns></returns>
        public bool SaveSel(string tempId, string pftId, string id, int userId, string userName)
        {
            _bcdProfiletypeUIRule = new BcdProfiletypeUIRule(new BcdProfiletypeService());
            //临时表信息添加到 工程项目竣工资料材料类型表
            AddProFileType(tempId, pftId, id, userId, userName);
            //获取待删除列表
            List<BcdProjectfiletempInfo> dList = _bcdProjectfiletempService.GetEntityList(" P_ID='" + tempId + "' and P_Fd_State=0 ");
            foreach (BcdProjectfiletempInfo dInfo in dList)
            {
                //删除
                List<BcdProfiletypeInfo> list = _bcdProfiletypeUIRule.GetEntityListBySql(" Pft_PIds like '%" + pftId + "%' and Fd_ID='" + dInfo.Fd_ID + "' ");
                if (list.Count > 0)
                    _bcdProfiletypeUIRule.DeleteEntity(list[0].Pft_ID);
            }

            //保存后清除临时表信息
            var ret = _bcdProjectfiletempService.DeleteByPID(tempId);
            return ret > 0;
        }

        /// <summary>
        /// 临时表信息添加到 工程项目竣工资料材料类型表
        /// </summary>
        /// <param name="tempId">临时表id</param>
        /// <param name="pftId">项目材料类型id</param>
        /// <param name="id">类型id</param>
        /// <param name="userId">登录人id</param>
        /// <param name="userName">登录人名称</param>
        public void AddProFileType(string tempId, string pftId, string id, int userId, string userName)
        {
            //获取添加列表
            List<BcdProjectfiletempInfo> aList = _bcdProjectfiletempService.GetEntityList(" P_ID='" + tempId + "' and Fd_PID='" + id + "' and (P_Fd_State is null or P_Fd_State=1)");
            _bcdProjectfileUIRule = new BcdProjectfileUIRule(new BcdProjectfileService());
            _bcdFiledataUIRule = new BcdFiledataUIRule(new BcdFiledataService());
            foreach (BcdProjectfiletempInfo aInfo in aList)
            {
                string newpid = Guid.NewGuid().ToString().ToUpper();
                if (aInfo.P_Fd_State == null)
                {
                    //添加
                    BcdProfiletypeInfo pinfo = _bcdProfiletypeUIRule.GetEntityById(new Guid(pftId));
                    BcdProfiletypeInfo info = new BcdProfiletypeInfo();
                    info.Pft_ID = newpid;
                    info.Pft_PID = pftId;
                    info.Pft_PIds = pinfo.Pft_PIds + ",'" + pinfo.Pft_ID + "'";
                    info.P_ID = pinfo.P_ID;
                    info.P_Name = pinfo.P_Name;
                    info.P_Code = pinfo.P_Code;
                    info.Pft_Section = pinfo.Pft_Section;
                    info.Pft_Unit = pinfo.Pft_Unit;
                    info.Pft_OperateUserID = userId;
                    info.Pft_OperateUserName = userName;
                    info.Pft_OperateTime = DateTime.Now;
                    info.Fd_Duty = pinfo.Fd_Duty;
                    BcdFiledataInfo tinfo = _bcdFiledataUIRule.GetEntityById(new Guid(aInfo.Fd_ID));
                    info.Fd_UType = tinfo.Fd_UType;
                    info.Fd_ID = tinfo.Fd_ID;
                    info.Fd_Name = tinfo.Fd_Name;
                    info.Fd_Order = tinfo.Fd_Order;
                    info.Fd_Numb = tinfo.Fd_Numb;
                    info.Fd_Code = tinfo.Fd_Code.Replace("项目", pinfo.P_Code).Replace("监理", pinfo.Pft_Section).Replace("标段", pinfo.Pft_Section);
                    if (tinfo.Fd_PIds.IndexOf("C1F9B4D4-9BF4-43D4-9AC9-CE3976F5A1AF") >= 0)
                    {
                        List<BcdProfiletypeInfo> pList = _bcdProfiletypeUIRule.GetEntityListBySql(" P_ID=" + info.P_ID + " and Pft_Section='" + info.Pft_Section + "' and Fd_ID='C1F9B4D4-9BF4-43D4-9AC9-CE3976F5A1AF' and Pft_ID in (" + info.Pft_PIds.Trim(',') + ") ");
                        if (pList.Count > 0)
                        {
                            string code = pList[0].Fd_Code;
                            info.Fd_Code = code + info.Fd_Code.Substring(code.Length);
                        }
                    }
                    info.Fd_Kit = tinfo.Fd_Kit;
                    info.Fd_Kept = tinfo.Fd_Kept;
                    info.Fd_Table = tinfo.Fd_Table;
                    info.Fd_Remark = tinfo.Fd_Remark;
                    //info.Fd_Important=tinfo.Fd_Important;
                    _bcdProfiletypeUIRule.InsertEntity(info);
                }
                else
                {
                    List<BcdProfiletypeInfo> list = _bcdProfiletypeUIRule.GetEntityListBySql(" Pft_PIds like '%" + pftId + "%' and Fd_ID='" + aInfo.Fd_ID + "' ");
                    if (list.Count > 0)
                        newpid = list[0].Pft_ID;
                }
                AddProFileType(tempId, newpid, aInfo.Fd_ID, userId, userName);
            }
        }
    }
}

